import { defineStore } from "pinia";
import { ref, reactive } from "vue";
// 引入env
const ENV = import.meta.env;
import { useRouterStore } from "./router";
import LocalStorage from "@/utils/localStorage";
import SessionStorage from "@/utils/sessionStorage";
import { ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
export const useUserStore = defineStore("user", () => {
    const VueRouter = useRouter();
    const token = ref(null)
    const userInfo = reactive({})
    const userRoles = ref([])
    const infoDialogShow = ref(false)
    const messageDialogShow = ref(false)
    // 是否登录
    const isLogin = ref(false)

    // 锁定状态
    const LockScreen = reactive({
        isLock: false, // 锁屏状态
        password: null, // 锁屏密码
        lock_time: null, // 锁屏时间
    })

    // 个性化配置


    const LoginForm = reactive({
        username: null, password: null
    })
    let {
        VITE_TOKEN_STORAGE,
        VITE_TOKEN_STORAGE_TYPE,
        VITE_TOKEN_STORAGE_KEY,
        VITE_USER_INFO_STORAGE,
        VITE_USER_INFO_STORAGE_TYPE,
        VITE_USER_INFO_STORAGE_KEY
    } = ENV;
    // 初始化token
    let initToken = null
    if (VITE_TOKEN_STORAGE == 'true') {
        if (VITE_TOKEN_STORAGE_TYPE == 'localStorage') {
            initToken = LocalStorage.Get(VITE_TOKEN_STORAGE_KEY)
        } else if (VITE_TOKEN_STORAGE_TYPE == "sessionStorage") {
            initToken = SessionStorage.Get(VITE_TOKEN_STORAGE_KEY)
        }
        token.value = initToken;
    }
    // 初始化用户信息
    let initUserInfo = null
    if (VITE_USER_INFO_STORAGE == 'true') {
        if (VITE_USER_INFO_STORAGE_TYPE == 'localStorage') {
            initUserInfo = LocalStorage.Get(VITE_USER_INFO_STORAGE_KEY)
        } else if (VITE_USER_INFO_STORAGE_TYPE == "sessionStorage") {
            initUserInfo = SessionStorage.Get(VITE_USER_INFO_STORAGE_KEY)
        }
        if (initUserInfo) {
            try {
                Object.assign(userInfo, initUserInfo);
                isLogin.value = true
            } catch (error) {
                isLogin.value = false
            }
        } else {
            isLogin.value = false
        }

    }
    // 初始化登录表单
    let initLoginForm = null
    if (true) {
        let key = "login_form"
        initLoginForm = LocalStorage.Get(key)
        Object.assign(LoginForm, initLoginForm);
    }
    // 设置token
    function SetToken(value) {
        token.value = value;
        if (VITE_TOKEN_STORAGE) {
            if (VITE_TOKEN_STORAGE_TYPE == 'localStorage') {
                LocalStorage.Set(VITE_TOKEN_STORAGE_KEY, value)
            } else if (VITE_TOKEN_STORAGE_TYPE == "sessionStorage") {
                SessionStorage.Set(VITE_TOKEN_STORAGE_KEY, value)
            }
        }
    }

    // 设置用户信息
    function SetUserInfo(data) {
        if (VITE_USER_INFO_STORAGE) {
            if (VITE_USER_INFO_STORAGE_TYPE == 'localStorage') {
                LocalStorage.Set(VITE_USER_INFO_STORAGE_KEY, data)
            } else if (VITE_USER_INFO_STORAGE_TYPE == "sessionStorage") {
                SessionStorage.Set(VITE_USER_INFO_STORAGE_KEY, data)
            }
        }
        Object.assign(userInfo, data);
    }

    // 设置用户登录表单
    function SetLoginForm(data) {
        let username = data.username
        let password = data.password
        if (username && password) {
            LoginForm.username = username
            LoginForm.password = password
            LocalStorage.Set("login_form", {
                username, password
            })
        }
    }
    // 设置用户角色
    function SetUserRoles(data) {
        userRoles.value = data
        LocalStorage.Set("userRoles", data);
    }
    // 退出登录
    function Logout() {
        // 删除用户信息
        SetUserInfo(null)
        // 删除用户token
        SetToken(null)
        // 跳转到登录页
        VueRouter.ToLogin(true)
    }
    // 弹出退出登录弹窗
    function LogoutDialog() {
        // 判断当前是否锁定,锁定则无法弹出
        if (LockScreen.isLock) {
            return
        }
        SetInfoDialog(false)
        ElMessageBox.confirm("是否确认退出登录，退出后将清除所有账号数据",
            "退出确认",
            {
                confirmButtonText: "登出",
                cancelButtonText: "我再想想",
                type: "warning",
            }
        ).then(() => {
            Logout()
        })
    }

    // 设置用户消息弹窗显示状态
    function SetMessageDialog(status: Boolean | undefined) {
        if (status === undefined) {
            status = !messageDialogShow.value
        }
        infoDialogShow.value = false
        messageDialogShow.value = Boolean(status)
    }
    // 设置用户信息弹窗显示状态
    function SetInfoDialog(status: Boolean | undefined) {
        if (status === undefined) {
            status = !infoDialogShow.value
        }
        messageDialogShow.value = false
        infoDialogShow.value = Boolean(status)
    }
    // 锁屏
    function Lock(Password) {
        messageDialogShow.value = false
        infoDialogShow.value = false
        LockScreen.isLock = true
        LockScreen.password = Password
    }
    // 解锁
    async function UnLock(Password, timeout = 1500) {
        let LockScreenPass = LockScreen.password
        let success = { code: 0, msg: "解锁成功" }
        let error = { code: 1, msg: "解锁失败" }
        if (LockScreenPass) {
            if (Password != LockScreenPass) {
                throw error
            }
        }
        setTimeout(() => {
            LockScreen.isLock = false
        }, timeout)
        return success
    }

    return {
        isLogin,
        UnLock,
        Lock,
        LockScreen,
        infoDialogShow,
        messageDialogShow,
        token,
        userInfo,
        LoginForm,
        SetToken, SetUserInfo, SetLoginForm, SetInfoDialog, SetMessageDialog,
        Logout,
        LogoutDialog
    }
});